using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DriveCar : MonoBehaviour
{
    [SerializeField] private Rigidbody2D _frontTireRB; // 前轮刚体
    [SerializeField] private Rigidbody2D _backTireRB; // 后轮刚体
    [SerializeField] private Rigidbody2D _carRB; // 后轮刚体
    [SerializeField] private float _speed = 150f; // 车辆速度
    [SerializeField] private float _rotationSpeed = 500f; // 旋转速度
    private float _moveInput; // 移动输入值

    // 每帧更新
    private void Update()
    {
        _moveInput = Input.GetAxisRaw("Horizontal"); // 获取水平输入
    }

    // 固定帧更新
    private void FixedUpdate()
    {
        // 给前轮和后轮施加扭矩，控制车辆运动
        _frontTireRB.AddTorque(-_moveInput * _speed * Time.fixedDeltaTime);
        _backTireRB.AddTorque(-_moveInput * _speed * Time.fixedDeltaTime);
        _carRB.AddTorque(-_moveInput * _rotationSpeed * Time.fixedDeltaTime);
    }
}
